# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import os
import re

class CsdnEduPipeline(object):
    def process_item(self, item, spider):
        dir = "D:\\python\pythonQuanZhanXueXi\scrapy\course"
        if (not os.path.exists(dir)):
            os.mkdir(dir)
        for i in range(0,len(item["title"])):
            link = item["link"]
            title = item["title"][i]
            number = item["number"][i]
            price = item["price"][i]
            time = item["time"][i]
            rst = r"[\/\\\:\*\?\<\>\|]"
            filename = re.sub(rst,"_",title)
            try:
                with open('D:\\python\pythonQuanZhanXueXi\scrapy\course\\'+filename+".txt", 'w',encoding="utf-8") as file:
                    file.write("课程链接:"+link+"\n")
                    file.write("课程名:" + title+"\n")
                    file.write("学员人数:" + number+"\n")
                    file.write("课程定价:" + price+"\n")
                    file.write("课时数:" + time+"节"+"\n")
            except Exception as err:
                print(err)
        return item
